<?php
header("Content-type: text/html;charset=utf-8");

include_once 'libs/PHPMailer/class.phpmailer.php';
require_once("model/contact.php");

if(isset($_POST['contact']))
{
    $email_from   = $_POST['email'];
    $company_name = $_POST['company'];
    $address      = $_POST['address'];
    $content      = $_POST['content'];
    $telephone    = $_POST['telephone'];

    $obj_contact =  new Contact();

    $obj_contact->set_companyName($company_name);

    // echo $obj_contact->get_companyName(); exit();

    if($obj_contact->insert_contact_into($company_name, $address, $email_from, $telephone, $content)){
        echo "success!";
    }
}


$mail = new PHPMailer();

$mail->IsSMTP();
                                      // set mailer to use SMTP
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "tls";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 587;
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "viet10932";  // SMTP username
$mail->Password = "anhviet17"; // SMTP password

$mail->From = $email_from;
$mail->FromName = $company_name;

$mail->addAddress("viet10932@gmail.com");

$mail->IsHTML(true); // set email format to HTML
$mail->Subject = utf8_encode("Contact");
$mail->Body    = "<ul><li>$content</li><li>$telephone</li></ul>"; // noi dung cua thu gui
$mail->AltBody = ""; // khong kem theo html

if(!$mail->Send())
{
    echo "Message could not be sent. <p>";
    echo "Mailer Error: " . $mail->ErrorInfo;
    exit;
}

echo "Message has been sent";